package com.oocl.im.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.im.dao.UserDao;
import com.oocl.im.pojo.User;
import com.oocl.im.util.DatabaseUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public int addUser(User user) {
		Connection connection = DatabaseUtil.getConnection();
		String sql = "insert into user2 values (user2_id_sequence.nextval,?,?)";
		PreparedStatement stat = null;
		try {
			stat = connection.prepareStatement(sql);
			stat.setString(1, user.getName());
			stat.setString(2, user.getPassword());
			return stat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null)
					connection.close();
				if (stat != null)
					stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int deleteUser(int id) {
		Connection connection = DatabaseUtil.getConnection();
		String sql = "delete from user2 where id=?";
		PreparedStatement stat = null;
		try {
			stat = connection.prepareStatement(sql);
			stat.setInt(1, id);
			return stat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null)
					connection.close();
				if (stat != null)
					stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int updateUser(User user) {
		Connection connection = DatabaseUtil.getConnection();
		String sql = "update user2 set name=?,password=? where id=?";
		PreparedStatement stat = null;
		try {
			stat = connection.prepareStatement(sql);
			stat.setString(1, user.getName());
			stat.setString(2, user.getPassword());
			stat.setInt(3, user.getId());
			return stat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null)
					connection.close();
				if (stat != null)
					stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public User find(int id) {
		Connection connection = DatabaseUtil.getConnection();
		String sql = "select * from user2 where id = ?";
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			stat = connection.prepareStatement(sql);
			stat.setInt(1, id);
			rs = stat.executeQuery();
			if (rs.next()) {
				User user = new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setPassword(rs.getString(3));
				return user;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null)
					connection.close();
				if (stat != null)
					stat.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public User findByName(String name) {
		Connection connection = DatabaseUtil.getConnection();
		String sql = "select * from user2 where name = ?";
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			stat = connection.prepareStatement(sql);
			stat.setString(1, name);
			rs = stat.executeQuery();
			if (rs.next()) {
				User user = new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setPassword(rs.getString(3));
				return user;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null)
					connection.close();
				if (stat != null)
					stat.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<User> findAll() {
		List<User> users = new ArrayList<User>();
		Connection connection = DatabaseUtil.getConnection();
		String sql = "select * from user2";
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			stat = connection.prepareStatement(sql);
			rs = stat.executeQuery();
			// extract infomation from resultset
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setPassword(rs.getString(3));
				users.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (connection != null)
					connection.close();
				if (stat != null)
					stat.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return users;
	}
}
